#!/bin/sh
# DMS_HOST=1为启用0为关闭DMS域名hosts文件写入。
DMS_HOST=1

DMS_HOST_URL="https://gitee.com/team_space/mynet_info/raw/master/dsm_hosts/hosts"
start_linenu=`cat /etc/hosts|awk '/fetch-DMS-hosts begin/ {printf NR}'`
hosts_linenu=`cat /etc/hosts|awk '/fetch-DMS-hosts/ {printf NR" "}'|awk '{print $1","$2}'`


if [ $DMS_HOST = 1 ]; then
    if [ "${hosts_linenu}" != "" ];then
        sed  -i "${hosts_linenu}d" /etc/hosts
    fi
    if [ "${#start_linenu}" != "0" ];then
        start_linenu=`expr ${start_linenu} - 1`
        curl $DMS_HOST_URL|while read line
        do
        sed -i "${start_linenu}a\\${line}" /etc/hosts;
        start_linenu=`expr ${start_linenu} + 1`
        done
    else
        start_linenu=2
        curl $DMS_HOST_URL |while read line
        do
        sed -i "${start_linenu}a\\${line}" /etc/hosts;
        start_linenu=`expr ${start_linenu} + 1`
        done
    fi
    

else
    if [ "${hosts_linenu}" != "" ];then
        sed  -i "${hosts_linenu}d" /etc/hosts
    fi
fi



#curl -L http://code.imnks.com/hosts-auto1007.sh | bash
### curl https://gitee.com/team_space/mynet_info/raw/master/DSM_host_update.sh |bash